package com.sanpu.kxe.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.trade.interfaces.ICompleteInputListener;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 收款页键盘
 */

public class KeyboardView extends LinearLayout implements View.OnClickListener{

    @Bind(R.id.seven_btn)
    Button mSevenBtn;
    @Bind(R.id.eight_btn)
    Button mEightBtn;
    @Bind(R.id.nine_btn)
    Button mNineBtn;
    @Bind(R.id.four_btn)
    Button mFourBtn;
    @Bind(R.id.five_btn)
    Button mFiveBtn;
    @Bind(R.id.six_btn)
    Button mSixBtn;
    @Bind(R.id.del_btn)
    ImageButton mDelBtn;
    @Bind(R.id.one_btn)
    Button mOneBtn;
    @Bind(R.id.two_btn)
    Button mTwoBtn;
    @Bind(R.id.three_btn)
    Button mThreeBtn;
    @Bind(R.id.point_btn)
    Button mPointBtn;
    @Bind(R.id.zero_btn)
    Button mZeroBtn;
    @Bind(R.id.ac_btn)
    Button mAcBtn;
    @Bind(R.id.receipt_btn)
    Button mReceiptBtn;

    private String mCurrentText = "";//当前输入的文案

    private ICompleteInputListener listener;

    public KeyboardView(Context context) {
        super(context);
        initView(context);
    }

    public KeyboardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public KeyboardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View mView = View.inflate(context, R.layout.view_keyboard, this);
        if (mView != null) {
            ButterKnife.bind(mView);
            initListener();
        }
    }

    @Override
    public void onClick(View v) {
        String input = v.getTag()==null?"":v.getTag().toString();
        switch (v.getId()) {
            case R.id.del_btn:
                back();
                break;
            case R.id.point_btn:
                inputPoint();
                break;
            case R.id.receipt_btn:
                listener.onCompletePay();
                break;
            case R.id.ac_btn:
                mCurrentText = "";
                listener.onCompleteInput(mCurrentText);
                break;
            default:
                inputNumber(input);
                break;
        }
    }

    private void inputNumber(String input) {
        if (!TextUtils.isEmpty(input)) {
            if (TextUtils.isEmpty(mCurrentText)) {
               mCurrentText =  input;
            } else if (TextUtils.equals("0", mCurrentText)) {
                if (TextUtils.equals("0", input)) {
                    return;
                } else {
                    mCurrentText = input;
                }
            } else if(mCurrentText.contains(".")) {
                String len = mCurrentText.substring(mCurrentText.indexOf("."), mCurrentText.length()-1);
                if (len.length()>=2){
                    return;
                } else {
                    mCurrentText += input;
                }
            } else {
                mCurrentText += input;
            }
        }
        listener.onCompleteInput(mCurrentText);
    }

    /**
     * 回退操作
     */
    private void back() {
        if (mCurrentText.length() > 1) {
            mCurrentText = mCurrentText.substring(0, mCurrentText.length() - 1);
        } else {
            mCurrentText = "";
        }
        listener.onCompleteInput(mCurrentText);
    }

    /**
     * 输入小数点
     */
    private void inputPoint() {
       if (TextUtils.isEmpty(mCurrentText)) {
           mCurrentText = "0.";
       } else if (!mCurrentText.contains(".")){
           mCurrentText += ".";
       }
       listener.onCompleteInput(mCurrentText);
    }

    private void initListener() {
        setViewTag(mSevenBtn, mEightBtn, mNineBtn, mFourBtn, mFiveBtn,
                mSixBtn, mOneBtn, mTwoBtn, mThreeBtn, mZeroBtn);
        mSevenBtn.setOnClickListener(this);
        mEightBtn.setOnClickListener(this);
        mNineBtn.setOnClickListener(this);
        mFourBtn.setOnClickListener(this);
        mFiveBtn.setOnClickListener(this);
        mSixBtn.setOnClickListener(this);
        mDelBtn.setOnClickListener(this);
        mOneBtn.setOnClickListener(this);
        mTwoBtn.setOnClickListener(this);
        mThreeBtn.setOnClickListener(this);
        mPointBtn.setOnClickListener(this);
        mZeroBtn.setOnClickListener(this);
        mAcBtn.setOnClickListener(this);
        mReceiptBtn.setOnClickListener(this);
    }

    /**
     * 设置tag
     * @param mBtn
     */
    private void setViewTag(Button... mBtn) {
        if (mBtn != null) {
            for (Button button:mBtn) {
                button.setTag(button.getText().toString());
            }
        }
    }

    public void setReceiptEnabled() {
        if (checkInvalid(mCurrentText)) {
            mReceiptBtn.setEnabled(true);
            mReceiptBtn.setBackgroundResource(R.drawable.selector_receipt_keyboard_checked);
        } else {
            mReceiptBtn.setEnabled(false);
            mReceiptBtn.setBackgroundResource(R.drawable.selector_receipt_keyboard_normal);
        }
    }

    /**
     * 验证输入文本是否有效
     *
     * @return
     */
    private boolean checkInvalid(String number) {
        boolean invalid = false;
        try {
            invalid = Float.parseFloat(number) != 0;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return invalid;
    }

    public void setListener(ICompleteInputListener listener) {
        this.listener = listener;
    }

    public void setCurrentText(String currentText) {
        this.mCurrentText = currentText;
    }
}
